
import markdownIt from "markdown-it"
const hljs = require('highlight.js')
import "./markdwon-it-css.js"
 
const md = new markdownIt({
  html: true,
  linkify: true,
  breaks: true,
  typographer: true,
  highlight: function (str, lang) {

    // let AppleStyle = `<svg  width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg>`
    let AppleStyle = `<svg  width="54" height="14" ><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg>`
    if (lang && hljs.getLanguage(lang)) {
      try {
        const preCode = hljs.highlight(lang, str, true).value;
        const lines = preCode.split(/\n/).slice(0, -1)
        let html = lines.map((item, index) => `<li><span class="line-num" data-line="${(index + 1)}"></span> ${item}</li>`).join('')
        html = `<ol language="${lang}">${html}</ol>`
        return `${AppleStyle}<pre class="hljs"><code>${html}</code></pre>`
      } catch (__) { return `${AppleStyle}` }
    }
    // 未添加代码语言，此处与上面同理
    const preCode = md.utils.escapeHtml(str)
    const lines = preCode.split(/\n/).slice(0, -1)
    let html = lines.map((item, index) => {
      return `<li><span class="line-num" data-line="${(index + 1)}"></span>${item}</li>`
    }).join('')
    html = `<ol>${html}</ol>`
    return `${AppleStyle}<pre class="hljs"><code>${html}</code></pre>`
  }

})
md.use(require('markdown-it-footnote'));//注脚
md.use(require("markdown-it-graphviz"));
md.use(require("markdown-it-collapsible"));
md.use(require('markdown-it-ins')) //++inserted++ => <ins>inserted</ins>
// md.use(require('markdown-it-katex') 
md.use(require('markdown-it-inline-comments'))//去掉注释
md.use(require('markdown-it-sub'))
md.use(require('markdown-it-sup'));
 
md.use(require('markdown-it-deflist'));
md.use(require('markdown-it-abbr'));
md.use(require('markdown-it-mark'));
md.use(require('markdown-it-task-lists')) //[ ] or [x].
md.use(require('markdown-it-images-preview'))
md.use(require('markdown-it-toc'))
md.use(require('markdown-it-emoji'))
md.use(require('markdown-it-front-matter'), (m)=>{
  let obj={}
  m.split("\n").forEach((v)=>{ 
 let temp=v.split(":");
obj[trim(temp[0])]=trim(temp[1])
})
GetFrontMatter=obj

})
function trim(data="") {
  return data.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
}




export let GetFrontMatter = {title:""} 
// markdown-it-code-webcomponents //解析markdown code 为组件
//  npm install markdown-it-code-embed --save //嵌入 https://codepen.io/ 代码
md.use(require("@junwatu/markdown-it-lazy-loading"), { img: "lazy" }); //延迟加载图片

md.use(require('markdown-web-copy'),{
    btnText: "⧉", // 'copy' | button text
    // failText: String, // 'copy fail' | copy-fail text
    successText: "", // 'copy success' | copy-success text
    // successTextDelay: 1000, // 2000 | successText show time [ms]
    extraHtmlBeforeBtn: "<a>", // '' | a html-fragment before <button>
    extraHtmlAfterBtn: "</a>", // '' | a html-fragment after <button>
    // showCodeLanguage: Boolean, // false | show code language before [btn || extraHtmlBeforeBtn] | [add-after-1.1.0]
    // attachText:"滋溜鹿博客", // '' | some text append copyText， Such as: copyright | [add-after-1.2.0]
})
// 指定规则
// function _wrap(render) {
//   return function () {
//     return `<div contenteditable="true" class="md-container" >${render.apply(this, arguments)}</div>` // eslint-disable-line
//   };
// }
// function _wrap2(render) {
//   return function () {
//     return `<div   class="md-container" >${render.apply(this, arguments)}</div>` // eslint-disable-line
//   };
// }
// var makeSafe = function (label) {
//   return label.replace(/[^\w\s]/gi, '').split(' ').join('_');
// };

// md.renderer.rules.heading_open = function (tokens, index) {
//   var level =  tokens[index].tag;
//   var label = tokens[index + 1];
//   if (label.type === 'inline') {
//     var anchor = makeSafe(label.content) + '_' + label.map[0];
//     return `<div contenteditable="true" class="md-container">`+'<' + level + '><a id="' + anchor + '"></a>';
//   } else {
//     return '</h1></div>';
//   }
// };

// // //  添加行号
// //   function injectLineNumbers(tokens, idx, options, env, slf) {
// //     var line;
// //     if (tokens[idx].map && tokens[idx].level === 0) {
// //       line = tokens[idx].map[0];
// //       tokens[idx].attrJoin('class', 'line');
// //       tokens[idx].attrSet('data-line', String(line));
// //     }
// //     return slf.renderToken(tokens, idx, options, env, slf);
// //   }

// // md.renderer.rules.heading_open =()=>{ return "<div class='md-container'>" }
// md.renderer.rules.heading_close =()=>{ return "</div>" }
// md.renderer.rules.code_inline= _wrap(md.renderer.rules.code_inline)
// md.renderer.rules.code_block = _wrap(md.renderer.rules.code_block)
// md.renderer.rules.image = _wrap2(md.renderer.rules.image)
// md.renderer.rules.fence = _wrap(md.renderer.rules.fence)

// md.renderer.rules.code_block= _wrap(md.renderer.rules.code_block)
// md.renderer.rules.hardbreak= _wrap(md.renderer.rules.hardbreak)
// md.renderer.rules.softbreak= _wrap(md.renderer.rules.softbreak)
// md.renderer.rules.text= _wrap(md.renderer.rules.text)
// md.renderer.rules.html_block= _wrap(md.renderer.rules.html_block)
// // md.renderer.rules.html_inline= _wrap(md.renderer.rules.html_inline)
// md.renderer.rules.collapsible_summary= _wrap(md.renderer.rules.collapsible_summary)
// md.renderer.rules.math_inline= _wrap(md.renderer.rules.math_inline)
// md.renderer.rules.math_block= _wrap(md.renderer.rules.math_block)
// md.renderer.rules.emoji= _wrap(md.renderer.rules.emoji) 
// md.renderer.rules.blockquote_open= _wrap(md.renderer.rules.blockquote_open)
// md.renderer.rules.blockquote= _wrap(md.renderer.rules.blockquote_open)

export default md
// export var  mdIt=md

// 见底部脚注[^hello] [^hello]: 一个注脚